package com.sgb.kjwl.view.ui.fragment.shop;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.core.widget.NestedScrollView;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.google.android.material.tabs.TabLayout;
import com.sgb.kjwl.AppLazyFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentShopMachineChildBinding;
import com.sgb.kjwl.di.shopping.DaggerShoppingFragComponent;
import com.sgb.kjwl.di.shopping.ShoppingFragModule;
import com.sgb.kjwl.model.entity.ShopLeaseEntity;
import com.sgb.kjwl.model.entity.TypeEntity;
import com.sgb.kjwl.model.entity.lease.AskLeaseWebsiteEntity;
import com.sgb.kjwl.view.ui.adapter.shopping.ShopMachineAdapter;
import com.sgb.kjwl.viewmodel.shopping.ShopMachinekChildModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.decoration.RecyclerCustomDecoration;
import com.swgk.core.recyclerview.decoration.RecyclerViewItemDecoration2;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
 * 企业网站-租赁经营
 */
public class ShopMachineChildFragment extends AppLazyFragment {
    @Inject
    ShopMachinekChildModel viewModel;
    private int mCurIndex = 0;
    private List<TypeEntity> mTypeData;
    private FragmentShopMachineChildBinding binding;
    private ShopMachineAdapter adapter;
    private SparseArray<List<ShopLeaseEntity>> mShopArray = new SparseArray<>();
    //    private String shopId;
    private String type;
    private String websiteNo;
    private String websiteType;
    private int childPosition = 0;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_shop_machine_child, container, false);
        return binding.getRoot();
    }

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerShoppingFragComponent.builder()
                .appComponent(appComponent)
                .shoppingFragModule(new ShoppingFragModule(this))
                .build()
                .inject(this);
    }

    public static ShopMachineChildFragment newInstance(String websiteNo,String websiteType, int childPosition) {
        ShopMachineChildFragment fragment = new ShopMachineChildFragment();
        Bundle bundle = new Bundle();
        bundle.putString("websiteNo", websiteNo);
        bundle.putString("websiteType", websiteType);
        bundle.putInt("childPosition", childPosition);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void initView() {
        websiteNo = getArguments().getString("websiteNo");
        websiteType = getArguments().getString("websiteType");
        childPosition = getArguments().getInt("childPosition");//暂时不跳转具体界面,勿删待定
        if (adapter == null) {
            adapter = new ShopMachineAdapter(getActivity(), null);
        }
        binding.rlvList.addItemDecoration(new RecyclerViewItemDecoration2(getResources().getDimension(R.dimen.dp10), getResources().getDimension(R.dimen.dp10)));
        binding.rlvList.setLayoutManager(new GridLayoutManager(getActivity(), 2));
        binding.rlvList.setAdapter(adapter);
        adapter.setOnItemClick(new AdapterOnItemClick<ShopLeaseEntity>() {
            @Override
            public void onItemClick(ShopLeaseEntity s, int position) {
                if (TextUtils.isEmpty(type)) return;
                //1机械,2设备,3车辆,4房产,5土地,6工厂,7农机
                viewModel.getH5Url(Integer.parseInt(type), s.getNo());

            }
        });

        binding.rlvList.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
               loadData(false);
            }

            @Override
            public void onLoadMore() {
                loadData(true);
            }
        });

        binding.tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                mCurIndex = tab.getPosition();
                type = mTypeData.get(mCurIndex).getType();
                List<ShopLeaseEntity> list = mShopArray.get(mCurIndex);
                if (list == null) {
                    loadData(false);
                } else {
                    adapter.setDatas(list);
                    binding.emptyView.setVisibility(list.size() > 0 ? View.GONE : View.VISIBLE);
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        setOnScrollChange();
    }

    @Override
    protected void onLazyLoad() {
        viewModel.groupBuyClassList(websiteNo,websiteType);
    }

    private void loadData(boolean isLoadMore){
        viewModel.queryListByStore(websiteNo, websiteType,type,isLoadMore);
    }

    // 获取租赁类型回调
    public void updateViews(List<TypeEntity> data) {
        if (data == null || data.size() == 0) {
            binding.tabLayout.setVisibility(View.GONE);
            empty();
            return;
        }
        mTypeData = data;
        mCurIndex = 0;
        for (int i = 0; i < data.size(); i++) {
            binding.tabLayout.addTab(binding.tabLayout.newTab().setText(data.get(i).getName()));
        }
        if (childPosition < data.size()) {
            mCurIndex = childPosition;
        } else {
            mCurIndex = 0;
        }
        binding.tabLayout.setScrollPosition(mCurIndex, 0, true);
        type = mTypeData.get(mCurIndex).getType();
        loadData(false);
    }

    public void refreshList(List<ShopLeaseEntity> stringList) {
        binding.emptyView.setVisibility(View.GONE);
        binding.rlvList.setVisibility(View.VISIBLE);
        binding.rlvList.setLoadingMoreEnabled(true);
        adapter.setDatas(stringList);
        binding.rlvList.refreshComplete();
        if (adapter.getItemCount() >= viewModel.total) {
            binding.rlvList.setLoadingMoreEnabled(false);
        }
    }


    public void loadMoreList(List<ShopLeaseEntity> stringList) {
        if(stringList==null){
            stringList = new ArrayList<>();
        }
        adapter.addDatas(stringList);
        binding.rlvList.loadMoreComplete();
        if (adapter.getItemCount() >= viewModel.total) {
            binding.rlvList.setLoadingMoreEnabled(false);
        }
    }

    public void requestComplete() {
        binding.rlvList.loadMoreComplete();
        binding.rlvList.refreshComplete();
    }

    public void empty() {
        binding.rlvList.refreshComplete();
        binding.emptyView.setVisibility(View.VISIBLE);
        binding.rlvList.setVisibility(View.GONE);
    }

    private void setOnScrollChange() {
        binding.nestedScrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
            @Override
            public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                //判断是否滑到的底部
                if (scrollY == (v.getChildAt(0).getMeasuredHeight() - v.getMeasuredHeight())) {
                    //调用刷新控件对应的加载更多方法
                    loadData(true);
                }
            }
        });

    }

}
